package 力扣_滑动窗口.常考;

/**
 * @author zx
 * @create 2023-03-06 20:22
 */
public class 长度最小的子数组_209 {
    public int minSubArrayLen(int target, int[] nums) {
        //滑动窗口
        int left = 0;//窗口左区间
        int sum = 0;
        int res = Integer.MAX_VALUE;
        for(int right = 0;right < nums.length;right++){
            sum = sum + nums[right];
            while(sum >= target){
                //更新返回值
                res = Math.min(res,right - left + 1);
                sum -= nums[left];
                left++;
            }
        }
        return res == Integer.MAX_VALUE ? 0 : res;
    }
}